package de.gameduell.framework.gui.util {
	import flash.display.Shape;
	import flash.display.Sprite;	

	public class Blocker {
		private var _clip:Sprite;
		private var _color:uint;
		private var _alpha:Number;
		private var _blocker:Shape;
		private var _sprite:Sprite;
		
		/**  
		 * @param blockedClip = the sprite the is to be blocked
		 * @param aColor      = color of the blocker
		 * @param aAlpha 	  = alphavalue of the blocker
		**/
		public function Blocker(blockedClip:Sprite,aColor:uint = 0xffffff,aAlpha:Number = 0) {		
			_clip   = blockedClip;	
			_color  = aColor;
			_alpha  = aAlpha;
			_sprite = new Sprite();
			_blocker = null;
			createShape();
		}
		
		public function bringToFront():void {
			_blocker.parent.setChildIndex(_blocker, _blocker.parent.numChildren-1);
		}
		
		/**  
		 * creates the shape of the blocker
		**/
		private function createShape():void {
			if(_blocker != null){
				if (_sprite.contains(_blocker)){
					_sprite.removeChild(_blocker);
				}
			}
			_blocker = new Shape();
			_blocker.graphics.beginFill(_color, _alpha);
			_blocker.graphics.drawRect(0, 0, _clip.width, _clip.height);
			_blocker.graphics.endFill();
			if (!_sprite.contains(_blocker)){
				_sprite.addChild(_blocker);
			}
		}
		
		/**  
		 * activates or deactivates the blocker
		**/
		public function set active(isActive:Boolean):void{
			if(isActive){
				_clip.addChild(_sprite);
			}else{
				if(_clip.contains(_sprite)){
					_clip.removeChild(_sprite);
				}
			}
		}
		
		/**  
		 * sets the color and the alphavalue of the blocker shape
		**/
		public function setColor(aColor:uint, aAlpha:Number = -99):void{
			_alpha = aAlpha == -99?_alpha:aAlpha;
			_color = aColor;
			createShape();
		}
		
		/**
		 * sets the alpha value of the blocker sprite i.e. of the blocker shape container
		 * @param aAlpha:Number alpha value
		 */
		public function set alpha(aAlpha:Number):void{
			_sprite.alpha = aAlpha;
		}
		
	}
}
